package cn.ac.ict.fpevaluation.cluster.ap;

import java.util.ArrayList;
import java.util.List;

import cn.ac.ict.fpevaluation.bean.AtomTrajectoryBean;
import cn.ac.ict.fpevaluation.cluster.ClassifyUtils;

/**
 * 对方向进行AP聚类
 *
 * @author Administrator
 */
public class APBasedDirections extends AP {
    /**
     * 构造函数
     *
     * @param data
     */
    public APBasedDirections(List<AtomTrajectoryBean> data) {
        super(data);
    }

    /**
     * 计算相似度
     *
     * @param data
     * @return
     */
    @Override
    protected double[][] calSimilarity(List<AtomTrajectoryBean> data) {
        int len = data.size();
        double[][] result = new double[len][len];
        pa = new ArrayList<Double>();
        for (int i = 0; i < len; i++) {
            double iOritation = ClassifyUtils.getAverageOritation(data.get(i).getOrientation());
            for (int j = i + 1; j < len; j++) {
                double jOritation = ClassifyUtils.getAverageOritation(data.get(j).getOrientation());

                double oritationDiff = Math.abs(iOritation - jOritation);

                //取负号是为了相差度数越小，相似度越大(反比关系)
                result[i][j] = -(oritationDiff < 180 ? oritationDiff : 360 - oritationDiff);

                pa.add(result[i][j]);// 存储相似度，用于计算中值
                result[j][i] = result[i][j];// 相似度是对称的
            }
        }

        // 对角线上元素赋中值
        for (int i = 0; i < trajectoryNum; i++)
            result[i][i] = calMidValue(pa) * 2;


        return result;
    }

    /**
     * 启动聚类
     */
    @Override
    public List<List<AtomTrajectoryBean>> launch() {
        return super.launch();
    }


    @Override
    public String getName() {
        return getClass().getName();
    }
}